package io.geekidea.springbootplus.system.interceptor;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.geekidea.springbootplus.framework.shiro.util.JwtTokenUtil;
import io.geekidea.springbootplus.framework.shiro.util.JwtUtil;
import io.geekidea.springbootplus.system.annotations.CurrentAdmin;
import io.geekidea.springbootplus.system.annotations.CurrentAdminInfo;
import io.geekidea.springbootplus.system.annotations.CurrentCarrierInfo;
import io.geekidea.springbootplus.system.entity.*;
import io.geekidea.springbootplus.system.service.AdminService;
import io.geekidea.springbootplus.system.service.CarrierService;
import io.geekidea.springbootplus.system.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;

@Slf4j
@ConditionalOnProperty(value = {"spring-boot-plus.interceptor.auth.enable"}, matchIfMissing = true)
public class AuthInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    CustomerService customerService;

    @Autowired
    AdminService adminService;

    @Autowired
    CarrierService carrierService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(!(handler instanceof HandlerMethod)){
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        Method method = handlerMethod.getMethod();
        String requestPath = request.getRequestURI();

        String token = JwtTokenUtil.getToken();
        String schoolID = JwtTokenUtil.getSchoolID();
        if (!StringUtils.isEmpty(token)) {
            String openID = JwtUtil.getUsername(token);

            Customer customer = customerService.getByOpenID(openID);

            CurrentUserInfo currentUserInfo = new CurrentUserInfo();

            if(customer != null){
                currentUserInfo.setCreateTime(customer.getCreateTime());
                currentUserInfo.setCustomerID(customer.getCustomerID());
                currentUserInfo.setOpenID(customer.getOpenID());
                currentUserInfo.setPersonName(customer.getPersonName());
                currentUserInfo.setPhone(customer.getPhone());
            }
            currentUserInfo.setSchoolID(schoolID);
            request.setAttribute("currentUser", currentUserInfo);

            Admin admin = adminService.getOne(new LambdaQueryWrapper<Admin>().eq(Admin::getOpenID, openID));
            School school = new School();
            school.setSchoolID(Integer.valueOf(schoolID));

            CurrentAdminInfo currentAdminUser = new CurrentAdminInfo();
            currentAdminUser.setAdmin(admin);
            currentAdminUser.setSchool(school);
            request.setAttribute("currentAdmin", currentAdminUser);

            Carrier carrier = carrierService.getOne(new LambdaQueryWrapper<Carrier>().eq(Carrier::getOpenID,openID));
            CurrentCarrierInfo currentCarrierInfo = new CurrentCarrierInfo();
            currentCarrierInfo.setCarrier(carrier);
            currentCarrierInfo.setSchool(school);
            request.setAttribute("currentCarrier", currentCarrierInfo);

        }else{
            CurrentCarrierInfo currentCarrierInfo = new CurrentCarrierInfo();

            CurrentAdminInfo currentAdminUser = new CurrentAdminInfo();

            if(!StringUtils.isEmpty(schoolID)){
                School school = new School();
                school.setSchoolID(Integer.valueOf(schoolID));
                currentAdminUser.setSchool(school);
                currentCarrierInfo.setSchool(school);
            }

            CurrentUserInfo currentUserInfo = new CurrentUserInfo();
            currentUserInfo.setSchoolID(schoolID);

            request.setAttribute("currentUser", currentUserInfo);
            request.setAttribute("currentAdmin", currentAdminUser);
            request.setAttribute("currentCarrier", currentCarrierInfo);

        }

        return true;
    }
}
